﻿using System;

namespace Tomato.Allure
{
	internal class SolutionImpl : Tomato.Allure.ISolution
	{
		private EnvDTE90.Solution3 m_instance;

		public Tomato.Allure.IProjects Projects
		{
			get 
			{
				if( m_instance.Projects != null )
				{
					return new ProjectsImpl( m_instance.Projects );
				}

				return null;
			}
		}

		public bool IsSaved
		{
			get { return m_instance.Saved; }
		}

		public SolutionImpl( EnvDTE.Solution instance )
		{
			
			m_instance = ( EnvDTE90.Solution3 )( instance );
		}

		public int ProjectCount
		{
			get { return m_instance.Count; }
		}

		public string FileName
		{
			get { return m_instance.FileName; }
		}

		public string FullName
		{
			get { return m_instance.FullName; }
		}

		public bool IsDirty
		{
			get { return m_instance.IsDirty; }
		}

		public bool IsOpen
		{
			get { return m_instance.IsOpen; }
		}

		public Tomato.Allure.IProject this[ int index ]
		{
			get
			{
				EnvDTE.Project project = m_instance.Item( index );
				if( project != null )
				{
					return new ProjectImpl( project );
				}

				return null;
			}
		}

		public Tomato.Allure.IProject this[ string projectName ]
		{
			get
			{
				EnvDTE.Project project = m_instance.Item( projectName );
				if( project != null )
				{
					return new ProjectImpl( project );
				}

				return null;
			}
		}

		public Tomato.Allure.IProject AddProjectFromFile( string projectFilePath, bool bNewSolution )
		{
			EnvDTE.Project project = m_instance.AddFromFile( projectFilePath, bNewSolution );
			if( project != null )
			{
				return new ProjectImpl( project );
			}

			return null;
		}

		public Tomato.Allure.IProject AddProjectFromTemplate( string templateFilePath, string destinationFolder, string projectName, bool bNewSolution )
		{
			EnvDTE.Project project = m_instance.AddFromTemplate( templateFilePath, destinationFolder, projectName, bNewSolution );
			if( project != null )
			{
				return new ProjectImpl( project );
			}

			return null;
		}

		public Tomato.Allure.IProject AddSolutionFolder( string folderName )
		{
			EnvDTE.Project project = m_instance.AddSolutionFolder( folderName );
			if( project != null )
			{
				return new ProjectImpl( project );
			}

			return null;
		}

		public void Close( bool bToSave )
		{
			m_instance.Close( bToSave );
		}

		public void Create( string destinationFolder, string solutionName )
		{
			m_instance.Create( destinationFolder, solutionName );
		}

		public Tomato.Allure.IProjectItem FindProjectItem( string projectItemName )
		{
			EnvDTE.ProjectItem projectItem = m_instance.FindProjectItem( projectItemName );
			if( projectItem != null )
			{
				return new ProjectItemImpl( projectItem );
			}

			return null;
		}

		public void Open( string solutionFilePath )
		{
			m_instance.Open( solutionFilePath );
		}

		public void RemoveProject( Tomato.Allure.IProject project )
		{
			try
			{
				EnvDTE.Project projectInstance = ( EnvDTE.Project )( project.GetInstance() );
				if( project != null )
				{
					m_instance.Remove( projectInstance );
				}
			}
			catch( Exception )
			{ }
		}

		public void SaveAs( string newSolutionFilePath )
		{
			m_instance.SaveAs( newSolutionFilePath );
		}
	}
}
